<?php

declare(strict_types=1);

namespace app\api\controller;

use app\common\model\OrderTemporary;
use app\common\tool\Result;
use think\facade\View;
use think\Response;

class Order extends Base
{
    /**
     *  获取商品提交信息
     */
    public function index()
    {
        try {
            $temporary = OrderTemporary::getTemporary($this->user->id);
            if ($temporary['code'] != 1) return Result::fail('信息不存在');
            return Result::fastReturn($temporary);
        } catch (\Exception $e) {
            return redirect('/');
        }
    }

    /**
     * 提交订单
     */
    public function submit(): Response
    {
        try {
            $result = OrderTemporary::submit($this->request->post(), $this->user);
            if ($result['code'] != 1) {
                return Result::fail($result['msg']);
            }
            return Result::fastReturn([
                'code' => 1,
                'msg'  => '订单已生成',
                'id'  => $result['data']
            ]);
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }

    /**
     * 获取待支付的订单
     */
    public function payment($id)
    {
        try {
            $info = OrderTemporary::getPayment($id, $this->user);
            if (empty($info['data'])) return Result::fail('订单信息不存在');
            return Result::fastReturn($info);
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }

    /**
     * 支付订单
     */
    public function confirmPayment(): Response
    {
        try {
            return Result::fastReturn(OrderTemporary::payment($this->request->post(), $this->user));
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    /**
     * 订单详情
     */
    public function my_order_details()
    {
        try {
            $param = $this->request->get();
            $info = OrderTemporary::where(['uid' => $this->user->id, 'id' => $this->request->get('id')])
                ->withoutField('update_time,is_del')
                ->with(['productsField', 'p', 'c', 'd', 'express'])
                ->append(['statusText'])
                ->find();
            if (empty($info)) return redirect('/');
            $info->statusCode = $info->getData('status');
            $info->friendlyText = getProductOrderFriendly($info->statusCode);
            return Result::success('获取成功！', $info);
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    /**
     * 订单列表
     */
    public function getList(): Response
    {
        try {
            $param = $this->request->get();
            $map = [['uid', '=', $this->user->id]];
            if (isset($param['type']) && $param['type']) {
                $map[] = ['status', '=', $param['type']];
            }

            $list = OrderTemporary::getPageList(
                $map,
                'id,sn,total_amount,real_amount,create_time,status,order_type',
                $this->getPage(),
                $this->getLimit(),
                false,
                ['productsField'],
                [],
                [],
                [],
                [],
                '',
                ['statusText']
            );
            foreach ($list['data'] as &$v) {
                $v->statusCode = $v->getData('status');
            }
            return Result::fastReturn($list);
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    /**
     * 订单状态修改
     */
    public function edit(): Response
    {
        try {
            return Result::fastReturn(OrderTemporary::edit($this->request->post(), $this->user));
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
}
